﻿onChat = undefined;
//将收到的消息进行时间戳封装，因为走的是storage key value change的event，所以必须保证每次的消息值不同
//返回{data:msg,time:xxx}
function dataAddTime(msg, json) {
    var obj = {
        data: json == 1 ? JSON.stringify(msg) : msg, time: new Date().getTime()
    }
    return JSON.stringify(obj);
}
function ws() {
    const connection = new signalR.HubConnectionBuilder().withUrl("/systemHub").build();
    connection.on("online", (msg) => {
        localStorage.setItem('chat_notice', dataAddTime('【' + msg + '】已上线...'));
    });
    connection.on("offline", (msg) => {
        localStorage.setItem('chat_notice', dataAddTime('【' + msg + '】已离线...'));
    });
    connection.on("oninfo", (msg) => {
        localStorage.setItem('chat_info', dataAddTime(msg));
    });
    connection.on("getChat", (msg) => {
        localStorage.setItem('chat_msg', dataAddTime(msg, 1));
    });

    connection.on("getPush", (msg) => {
        debugger;
    });
    connection.on("getItem", (data) => {
        debugger;
    });
    connection.on("execute", (re) => {
        debugger;
        eval(re);
    });
    connection.on("refresh", () => {
        debugger;
    });
    connection.onclose(() => {
        debugger;
        setTimeout(ws, 30000)
    });
    connection.start().catch(err => {
        console.error(err.toString());
        setTimeout(ws, 30000)
    });
}
function setHandler(fn) {
    debugger;
    if (typeof fn == 'function') {
        onChat = fn;
    }
}